<template>
    <div class="reset-password">
        <div class="login-form">
            <div class="form-item">
                <van-field
                    :value="ruleForm.oldPassword"
                    @change="handleInput1"
                    placeholder="请输入旧密码"
                />
            </div>
            <div class="form-item">
                <van-field
                    :value="ruleForm.password"
                    @change="handleInput2"
                    placeholder="请输入新密码"
                />
            </div>
            <div class="form-item">
                <van-field
                    :value="ruleForm.password2"
                    @change="handleInput3"
                    placeholder="确认新密码"
                />
            </div>
            <div class="submit-btn" @click="handleSubmit">确认</div>
        </div>
        <div class="tips-box">
            <div class="tips-text" @click="handleLogin">登陆</div>
            <div class="tips-text" @click="handleOpenServiceDialog">联系客服</div>
        </div>
        <van-dialog id="van-dialog" confirm-button-color="#5581FA"/>
    </div>
</template>

<script>
import {getServiceConf, userRequest} from "@/api";
import Dialog from '@/wxcomponents/vant/dist/dialog/dialog';

export default {
    name: "resetPassword",
    data() {
        return {
            ruleForm: {
                oldPassword: '',
                password: '',
                password2: ''
            },
            serviceConf: {}
        }
    },
    methods: {
        handleInput1(e) {
            this.ruleForm.oldPassword = e.detail
        },
        handleInput2(e) {
            console.log(e)
            this.ruleForm.password = e.detail
        },
        handleInput3(e) {
            console.log(e)
            this.ruleForm.password2 = e.detail
        },
        handleSubmit() {
            console.log('this.ruleForm',this.ruleForm)
            let isEmpty = false
            let message = ''
            if (!this.ruleForm.oldPassword.trim()) {
                message = '旧密码不能为空'
                isEmpty = true
            } else if (!this.ruleForm.password.trim()){
                message = '新密码不能为空'
                isEmpty = true
            } else if (!this.ruleForm.password2.trim()){
                message = '确认新密码不能为空'
                isEmpty = true
            }
            if (isEmpty) {
                uni.showToast({
                    title: message,
                    icon: "error",
                    duration: 3000
                })
                return
            }
            userRequest.updatePassword({
                newPassword: this.ruleForm.password,
                oldPassword: this.ruleForm.oldPassword
            }).then(res => {
                uni.showToast({
                    title: '修改成功',
                    icon: "success",
                    duration: 3000
                })
                uni.redirectTo({
                    url: '/pages/tabBar/my/login'
                })
            })
        },
        // 登陆
        handleLogin() {
            uni.redirectTo({
                url: '/pages/tabBar/my/login'
            })
        },
        // 打开客服对话框
        async handleOpenServiceDialog() {
            // on close
            if (!this.serviceConf.mobile) {
                const res = await getServiceConf()
                this.serviceConf = res.data
            }
            await Dialog.alert({
                message: `客服联系方式：${this.serviceConf.mobile}`,
            })
        },
    }
}
</script>

<style lang="less">
.reset-password {
    width: 100vw;
    height: 100vh;
    padding: 16px 24px;
    overflow: hidden;
    background-color: #FFFFFF;
    .form-item {
        width: 100%;
        border-radius: 6px;
        color: #AEB3C0;
        height: 48px;
        line-height: 48px;
        display: flex;
        align-items: center;
        border: 1px solid #E4E4E4;
        margin-bottom: 30px;
        .phcolor {
            color: #AEB3C0;
            font-size: 14px;
        }
    }
    .submit-btn {
        width: 100%;
        height: 48px;
        border-radius: 6px;
        background-color: #5581FA;
        color: #FFFFFF;
        text-align: center;
        line-height: 48px;
        font-size: 14px;
    }
    .tips-box {
        display: flex;
        font-size: 14px;
        color: #AEB3C0;
        padding-top: 20px;
        .tips-text {
            flex: 1;
            text-align: center;
        }
    }
}

</style>